﻿// -----------------------------------------------------------------------
// <copyright file="Class1.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------


namespace WebServicesTesting.Automation
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Channels;
    using System.ServiceModel;
    using System.ServiceModel.Description;

    public class ResponseXmlInterceptingBehavior : IEndpointBehavior
    {
        // Fields
        public ResponseXmlInterceptingInspector ResponseXmlInterceptor { get; private set; }

        // Methods
        public ResponseXmlInterceptingBehavior()
        {
            this.ResponseXmlInterceptor = new ResponseXmlInterceptingInspector();
        }

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(this.ResponseXmlInterceptor);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }       

        public void Validate(ServiceEndpoint endpoint)
        {
        }        

        // Nested Types
        public class ResponseXmlInterceptingInspector : IClientMessageInspector
        {
            public string RequestXml { get; set; }
            public string ResponseXml { get; set; }       

            public void AfterReceiveReply(ref Message reply, object correlationState)
            {
                if (reply != null)
                {
                    this.ResponseXml = reply.ToString();
                }
            }

            public object BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                if (request != null)
                {
                    this.RequestXml = request.ToString();
                }

                return request;
            }       
        }
    }



}
